package com.qf.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import com.qf.bean.Employee;
import com.qf.bean.Menu;
import com.qf.bean.Role;
import com.qf.dao.EmployeeMapper;
import com.qf.dao.MenuMapper;
import com.qf.dao.RoleMapper;
import com.qf.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * IntelliJ IDEA -> person
 * 2019/11/20 0:10
 *
 * @author zmyao
 * 别看，我是渣渣。
 */
@Service
public class EmployeeServiceImpl  implements EmployeeService {
    @Autowired
private EmployeeMapper employeeMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public Employee login(String loginname, String loginpassword) {
        Employee employee = employeeMapper.login(loginname);
        if (employee != null ){
            if (employee.getLoginpassword().equals(loginpassword)){
                //获取用户d对应的角色
                Integer roleid = employee.getRoleid();
                Role role = roleMapper.selectByPrimaryKey(roleid);
                //在这里获取角色对应的菜单信息
//                List<Menu> menus = menuMapper.selectByRoleId(roleid);//未分级的菜单
                List<Menu> menus = menuMapper.selectMenusByRoleId(role.getRoleid());//未分级的菜单
                List <Menu> firstMenus = new ArrayList<>();
                for (Menu menu : menus) {
                    if (menu.getUpmenuid() == 0){//一级菜单
                        List<Menu>   secondMenus = new ArrayList<>();
                        for (Menu second : menus) {
                            if (second.getUpmenuid() == menu.getMenuid()){//二级菜单
                                secondMenus.add(second);
                            }
                        }
                        menu.setSecondMenu(secondMenus);
                        firstMenus.add(menu);
                    }
                }
                role.setMenuList(firstMenus);
                List<Menu> permissionList = menuMapper.selectPermissionByRoleId(role.getRoleid());
                role.setPermissionList(permissionList);

                employee.setRole(role);
                return  employee;
            }
            return  null;
        }
        return null;
    }

    @Override
    public PageInfo<Employee> findAll(int index, int pageSize,String username,String rname,String deptname ) {
        if (username != null ){
            username= username.trim();
        }
        if (rname != null ){
            rname= rname.trim();
        }
        if (deptname != null ){
            deptname= deptname.trim();
        }
        PageHelper.startPage(index, pageSize);
        Map map = new HashMap();
        map.put("username",username);
        map.put("rolename",rname);
        map.put("deptname",deptname);
        System.out.println( "0"+ rname);
        List<Employee> employeeList = employeeMapper.findAll(map);
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);
        return pageInfo;
    }
@Transactional
    @Override
    public int insert(Employee employee) {
        int len = 0;
        try {
            len = employeeMapper.insertSelective(employee);
        }catch (DuplicateKeyException e ){
            len = -1;
        }catch (Exception e){
            len =-2;
        }
    return len;
    }

    @Override
    public Employee findByLoginname(String loginname) {
        return employeeMapper.findByLoginname(loginname);
    }

    @Override
    public Employee selectByPrimaryKey(Integer id) {
        return employeeMapper.selectByPrimaryKey(id);
    }
@Transactional
    @Override
    public int updateEmp(Employee employee) {
        return employeeMapper.updateByPrimaryKeySelective(employee);
    }

    @Transactional
    @Override
    public int deleteByid(int id,int state ) {
        Map map = new HashMap();
        map.put("id",id);
        map.put("state",state);
        return employeeMapper.updateStateById(map);
    }
@Transactional
    @Override
    public int deleteByIds(int[] empids) {
        return employeeMapper.deleteByIds(empids);
    }

    @Override
    public List<Employee> findByIds(int[] empids) {
        return   employeeMapper.findByIds(empids);

    }
}
